import datetime

class DateTimeUtils:
    @classmethod
    def get_current_datetime_str(cls):
        """
        获取当前日期
        :return:
        """
        return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    @classmethod
    def get_date_start_end(cls, date):
        """
        获取一天的开始和结束
        :param date: 日期
        """
        return f"{date} 00:00:00" , f"{date} 23:59:59"

    @classmethod
    def convert_to_datetime(cls, value):
        if isinstance(value, str):
            if ':' in value and len(value) > 16:
                value = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
            elif ':' in value and len(value) == 16:
                value = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M')
            else:
                value = datetime.datetime.strptime(value, '%Y-%m-%d')
        return value
    
    @classmethod
    def get_week_day(cls,date_str):
        """获取某天是周几

        :param date_str: 时间字符串
        :return: int 1表示周一 2表示周二，以此类推
        """
        date = cls.convert_to_datetime(date_str)
        week_map = {
            "Monday":1,
            "Tuesday":2,
            "Wednesday":3,
            "Thursday":4,
            "Friday":5,
            "Saturday":6,
            "Sunday":7
        }
        return week_map.get(date.strftime("%A"))

    @classmethod
    def get_date_by_week_number(cls,date_str,week_number):
        """从date_str往后获取最近的周几

        :param date_str: 日期
        :param week_number: int 星期几对应的number
        :return: str '%Y-%m-%d %H:%M:%S'
        """
        get_result = False
        day_list = [0,1,2,3,4,5,6,7]
        for _day in day_list:
            _date = cls.add_days(date_str,_day)
            _week_number = cls.get_week_day(_date)
            if _week_number == week_number:
                return _date

    @classmethod
    def add_days(cls, date_str, days):
        date = cls.convert_to_datetime(date_str)
        res_date = date + datetime.timedelta(days=days)
        return res_date.strftime("%Y-%m-%d %H:%M:%S")
